package com.jihox.abookpro.uploader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.zip.ZipOutputStream;

public class MyZip {

	@SuppressWarnings("resource")
	public static void createZip(File zipFile, Vector<File> files)
			throws Exception {
		FileOutputStream os = null;
		FileInputStream is = null;
		try {
			
			String path = zipFile.getParent();
			
			File dstDir = new File(path);
			if (!dstDir.exists()){
				dstDir.mkdir();
			}
		
			os = new FileOutputStream(zipFile);
			ZipOutputStream zos = new ZipOutputStream(os);
			{
				for (File file : files) {
					if (!file.exists()) {
						throw new Exception("file not exist:" + file.getPath());
					}
					java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(
							file.getName());
					is = new FileInputStream(file);
					zos.putNextEntry(zipEntry);

					int len;
					byte[] buffer = new byte[4096];

					while ((len = is.read(buffer)) != -1) {
						zos.write(buffer, 0, len);
					}

					zos.closeEntry();
					is.close();
					is = null;
				}
			}
			{
				zos.finish();
				zos.close();
				os.close();
				os = null;
			}
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {
			}
			try {
				if (os != null)
					os.close();
			} catch (IOException e) {
			}
		}
	}

}
